﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 畜牧业审核程序国产.ViewModels
{
    public static class TaskExtensions
    {
        public static async void SafeFireAndForget(this Task task, Action<Exception> onException = null)
        {
            try
            {
                await task;
            }
            catch (Exception ex)
            {
                onException?.Invoke(ex);
            }
        }
    }
    internal partial class Page9MarketInfoViewModel : ViewModelBase
    {
        [ObservableProperty]
        private string _internalFatPrice = null;
        [ObservableProperty]
        private string _outsideFatPrice = null;
        [ObservableProperty]
        private string _localFatPrice = null;
        [ObservableProperty]
        private string _soybeansPrice = null;
        [ObservableProperty]
        private string _cornPrice = null;
        [ObservableProperty]
        private string _pigGrainRatio = null;
        [ObservableProperty]
        private string _pigletPrice = null;
        [ObservableProperty]
        private string _sowPrice = null;
        [ObservableProperty]
        Dictionary<string, string> _priceInfo = null;
        public RelayCommand comd
        { get; set; }
        public Page9MarketInfoViewModel()
        {
            this.comd = new RelayCommand(async () => await 
                InitializeAsync());
            //InitializeAsync().SafeFireAndForget();
            //InternalFatPrice = PriceInfo["内三元"];
            //OutsideFatPrice = PriceInfo["外三元"];
            //LocalFatPrice = PriceInfo["土杂猪"];
            //CornPrice = PriceInfo["玉米"];
            //SoybeansPrice = PriceInfo["豆粕"];
            //PigGrainRatio = PriceInfo["猪粮比"];
        }

        private async Task InitializeAsync()
        {

            PriceInfo = await Models.ToolsClass.MySpider.GetPriceAsync();
            InternalFatPrice = PriceInfo["内三元"];
            OutsideFatPrice = PriceInfo["外三元"];
            LocalFatPrice = PriceInfo["土杂猪"];
            CornPrice = PriceInfo["玉米"];
            SoybeansPrice = PriceInfo["豆粕"];
            PigGrainRatio = PriceInfo["猪粮比"];
            PigletPrice = PriceInfo["仔猪集市价格"];
            SowPrice = PriceInfo["全国二元母猪销售价格"];
        }
    }
}
